<template>
  <div class="container">
    <nav-bar
      title="星级评定区域分布情况"
      isCustomLeftBtn
      @leftClick="back"
    ></nav-bar>
    <div class="search-box">
      <div class="left-box">
        <pub-area
        :maxSelectLevel="3"
            @change="changeArea"
            customRightIcon
            label=""
            :isLink="false"
            valueIsLast
        ></pub-area>
        <!-- <pub-date @sureDate="sureDate"></pub-date> -->
      </div>
    </div>
    <div class="tab-box">
      <pub-tab @change="changeTab" :tab-list="state.tabList"></pub-tab>
    </div>
    <ul class="content-box">
      <li v-for="(item, i) in state.info" :key="i">
        <div class="top-text">
          <div class="label">{{ item.name }}</div>
          <div class="value">
            <count-to
              :startVal="0"
              :endVal="Number(item[ratingName])"
              :decimals="0"
              :duration="1500"
            ></count-to>
            家
          </div>
        </div>
        <div class="bottom-line">
          <div
            class="inner-line"
            :style="{ width: (item.num / maxNum) * 100 + '%' }"
          ></div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script setup>
import { ref, reactive, inject, onBeforeMount } from 'vue'
import { CountTo } from 'vue3-count-to'
import { get_area_start } from '@/api/xcc'
import { useUserStore } from '@/pinia/modules/user'

const changeComponents = inject('changeComponents')
const state = reactive({
  form: {
    areaCode: useUserStore().userLastArea
  },
  info: [],
  tabList: [
    '星级机构',
    '五星机构',
    '四星机构',
    '三星机构',
    '二星机构',
    '一星机构'
  ]
})
const ratingName = ref('rating1')
const maxNum = ref(0)
const maxNum1 = ref(0)
const maxNum2 = ref(0)
const maxNum3 = ref(0)
const maxNum4 = ref(0)
const maxNum5 = ref(0)
const active = ref(0)
const back = () => {
  changeComponents('view')
}

const changeTab = async val => {
  let obj = {
    1: 5,
    2: 4,
    3: 3,
    4: 2,
    5: 1
  }
  state.form.level = val ? obj[val] : ''
  await getAreaData()
  ratingName.value =
    state.form.level == 1
      ? 'rating1'
      : state.form.level == 2
      ? 'rating2'
      : state.form.level == 3
      ? 'rating3'
      : state.form.level == 4
      ? 'rating4'
      : 'rating5'

}

const sureDate = val => {
  state.form.date = val[0]
  getAreaData()
}

const changeArea = val => {
  state.form.areaCode = val
  getAreaData()
}

const getAreaData = async () => {
  let res = await get_area_start(state.form)
  if (res.succeed) {
    let numArr1 = []
    let numArr2 = []
    let numArr3 = []
    let numArr4 = []
    let numArr5 = []
    res.data.forEach(element => {
      numArr1.push(Number(element.rating1))
      numArr2.push(Number(element.rating2))
      numArr3.push(Number(element.rating3))
      numArr4.push(Number(element.rating4))
      numArr5.push(Number(element.rating5))
    })
    state.info = res.data
    setTimeout(() => {
      maxNum1.value = Math.max.apply(null, numArr1)
      maxNum2.value = Math.max.apply(null, numArr2)
      maxNum3.value = Math.max.apply(null, numArr3)
      maxNum4.value = Math.max.apply(null, numArr4)
      maxNum5.value = Math.max.apply(null, numArr5)
      maxNum.value = maxNum1.value
    }, 500)
  }
}

onBeforeMount(() => {
  getAreaData()
})
</script>
<style lang="less" scoped>
.container {
  height: 100%;
  .search-box {
    display: flex;
    height: 90px;
    width: 100%;
    border-bottom: 1px solid #e6e6e6;
    justify-content: space-between;
    background-color: #fff;
    .left-box {
      display: flex;
      align-items: center;
      height: 100%;
      :deep(.van-cell) {
        width: 220px;
        height: 100%;
        input {
          // max-width: 400px;
        }
      }
    }
    .right-box {
      height: 100%;
      margin-right: 40px;
      .text {
        line-height: 90px;
        font-size: 30px;
        font-weight: 400;
        color: #4375e4;
      }
    }
  }
  .tab-box {
    padding: 30px 0;
    background-color: #fff;
  }
  .content-box {
    background-color: #fff;
    padding: 30px 40px;
    overflow: auto;
    height: calc(100% - 364px);
    li {
      margin: 0px 0 40px 0;
      .top-text {
        display: flex;
        justify-content: space-between;
        .label {
          font-size: 30px;
        }
        .value {
          font-size: 30px;
          font-family: Microsoft YaHei;
        }
      }
      .bottom-line {
        margin-top: 20px;
        width: 100%;
        height: 10px;
        border-radius: 5px;
        .inner-line {
          width: 0%;
          transition: 1000ms;
          height: 100%;
          border-radius: 5px;
          background-color: #2588ff;
        }
      }
    }
  }
}
</style>
